package com.dyz.leetcode._08_StackAndQueue;

import java.util.ArrayDeque;

public class _20_isValid {
    public boolean isValid(String s) {
        if (s.length()%2!=0){return false;}
        ArrayDeque<Character> stack = new ArrayDeque<>();
        for(char c:s.toCharArray()){
            if(c=='('||c=='['||c=='{'){
                stack.push(c);
            }else {
                if(stack.isEmpty()){return false;}
                else {
                    char cur = stack.pop();
                    if( c==')' && cur!='('){return false;}
                    if(c=='}' && cur!='{'){return false;}
                    if(c==']' && cur!='['){return false;}
                }
            }
        }
        return stack.isEmpty();
    }

}
